package com.own.demo.长沙空中路网格子.Thread;

import org.locationtech.jts.geom.Coordinate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * 线程启用接口
 *
 * @author heguanglong
 * @date 2023/6/13
 */
public class PointLonThread implements Runnable {
    private static Logger log = LoggerFactory.getLogger(PointLonThread.class);
    private Map<String, List<Coordinate>> xMap;
    private List<Double> lonList;
    private CountDownLatch countDownLatch;
    private List<Coordinate> allPoints;
    private int allPointsLon;
    public PointLonThread(Map<String, List<Coordinate>> xMap, CountDownLatch countDownLatch, List<Double> lonList, List<Coordinate> allPoints, int allPointsLon) {
        this.countDownLatch = countDownLatch;
        this.lonList=lonList;
        this.xMap=xMap;
        this.allPoints=allPoints;
        this.allPointsLon=allPointsLon;
    }

    @Override
    public void run() {

        try {
            int lonListRow = lonList.size();
            for (int i = 0; i < lonListRow; i++) {
                List<Coordinate> data = new ArrayList<>(1024);
                String tempLon = lonList.get(i) + "";
                for (int j = 0; j < allPointsLon; j++) {
                    Coordinate coordinate = allPoints.get(j);
                    String x = coordinate.x + "";
                    if (tempLon.equals(x)) {
                        data.add(coordinate);
                    }
                }

                data = data.stream().distinct().collect(Collectors.toList());
                data = data.stream().sorted(Comparator.comparing(Coordinate::getY)).collect(Collectors.toList());
                xMap.put(tempLon, data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        countDownLatch.countDown();
    }

}
